<?php
namespace app\wechat\controller;

use app\admin\model\system\Debug;
use app\wechat\service\Open;
use Curl\CurlHttpRequest;
use EasyWeChat\Factory;
use think\Exception;
use think\facade\Log;
use think\Request;

class Oauth
{
    /**
     * Notes:网页授权回调地址
     * User: chenping
     * email: 375129701@qq.com
     */
    public function callback( )
    {
        $config = get_option("wechat_official");
        $config = array_merge($config['official'],config("official."));
        $app    = Factory::officialAccount($config);
        $oauth  = $app->oauth;
        $user = $oauth->user();
        session('wechat_user',$user->toArray());
        $targetUrl = empty(session('target_url')) ? '/' : session('target_url');
        if($targetUrl)
        {
            header('location:'. $targetUrl);
        }
    }

    public function opencallback()
    {
        $config = get_option("wechat_official");
        $openConfig = $config['open'];
        return (new Open($openConfig))->service();
    }

    public function wxcallback()
    {
        $code = request()->param("code");
        $config = get_option("wechat_official");
        $open = new Open($config['open']);
        $access_token = $open->access_token($code);
        $user = $open->get_userinfo($access_token);
        if($user)
        {
            session('wechat_user',$user);
        }else{
            return [];
        }
    }



}